<?php 
/*
 module:		财务报告控制器
 create_time:	2024-02-20 11:34:19
 author:		
 contact:		
*/

namespace app\admin\controller;
use think\exception\ValidateException;
use app\admin\model\Finance as FinanceModel;
use think\facade\Db;

class Finance extends Admin {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if (!$this->request->isPost()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$page = $this->request->post('page', 1, 'intval');

			$where = [];
			$where['finance_id'] = $this->request->post('finance_id', '', 'serach_in');
			$where['username'] = $this->request->post('username', '', 'serach_in');
			$where['name'] = $this->request->post('name', '', 'serach_in');
			$where['mobile'] = $this->request->post('mobile', '', 'serach_in');
			$where['money'] = $this->request->post('money', '', 'serach_in');
			$where['mend_money'] = $this->request->post('mend_money', '', 'serach_in');
			$where['user_id'] = $this->request->post('user_id', '', 'serach_in');
			$where['caiwu_test'] = $this->request->post('caiwu_test', '', 'serach_in');

			$field = 'finance_id,username,name,mobile,money,mend_money,date,alert_date,caiwu_test';

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

			$orderby = ($sort && $order) ? $sort.' '.$order : 'finance_id desc';

			$query = FinanceModel::field($field);

			$res =$query->where(formatWhere($where))->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			$data['sum_money'] = $query->where(formatWhere($where))->sum('money');
			$data['sum_mend_money'] = $query->where(formatWhere($where))->sum('mend_money');
			return json($data);
		}
	}


	/*
 	* @Description  修改排序开关
 	*/
	function updateExt(){
		$postField = 'finance_id,';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(!$data['finance_id']) throw new ValidateException ('参数错误');
		FinanceModel::update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->post('finance_id', '', 'serach_in');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'finance_id,username,name,mobile,money,mend_money,date,alert_date,caiwu_test';
		$res = FinanceModel::field($field)->findOrEmpty($id);

		if($res->isEmpty()){
			throw new ValidateException ('信息不存在');
		}

		return json(['status'=>200,'data'=>$res]);
	}



/*start*/
	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'finance_id,name,mobile,money,mend_money';
		$data = $this->request->only(explode(',',$postField),'post',null);
		
		$data['alert_date'] = time();

		$this->validate($data,\app\admin\validate\Finance::class);

		try{
			FinanceModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}
/*end*/


/*start*/
	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'name,mobile,money,mend_money';
		$data = $this->request->only(explode(',',$postField),'post',null);
		
		$admin = session('admin');
	    $data['username'] = $admin['name'].'('.$admin['role_name'].')';
	    
	    $data['date'] = time();
	    
		$this->validate($data,\app\admin\validate\Finance::class);

		try{
			$res = FinanceModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}
/*end*/

 /*start*/
    function money_count(){
        $user = session('admin');
    
		$field = 'finance_id,username,name,mobile,money,mend_money,date,alert_date';

		$query = FinanceModel::field($field);

		$data['status'] = 200;
		$data['sum_money'] = $query->where(formatWhere($where))->sum('money');
		$data['sum_mend_money'] = $query->where(formatWhere($where))->sum('mend_money');
		return json($data);
        
    }

     /*end*/



}

